let userListModule = (function () {
	let $deleteAll = $('.deleteAll'),
		$selectBox = $('.selectBox'),
		$searchInp = $('.searchInp'),
		$tableBox = $('.tableBox'),
		$tbody = $tableBox.find('tbody'),
		$checkAll = $('#checkAll'),
		$checkList = null;

	// 获取部门列表信息，把获取到的信息绑定到
	let queryDepartmentList = async function () {
		let result = _checkStore('departmentList', 60 * 60 * 1000);
		if (result === false) {
			result = await axios.get('/department/list');
			if (parseInt(result.code) === 0) {
				result = result.data;

				// 存储到本地做临时缓存
				_store('departmentList', result);
			}
		}
		let str = ``;
		result.forEach(item => {
			str += `<option value="${item.id}">${item.name}</option>`;
		});
		$selectBox.append(str);
	};

	// 获取员工列表信息
	let queryUserList = async function () {
		let departmentId = $selectBox.val() || 0,
			search = $searchInp.val().trim();
		let result = await axios.get('/user/list', {
			params: {
				departmentId,
				search
			}
		});
		if (parseInt(result.code) === 0) {
			let str = ``;
			_each(result.data, item => {
				let {
					id,
					name,
					sex,
					email,
					phone,
					department,
					job,
					desc
				} = item;
				str += `<tr>
					<td class="w3"><input type="checkbox" userId="${id}"></td>
					<td class="w10">${name}</td>
					<td class="w5">${parseInt(sex) === 0 ? '男' : '女'}</td>
					<td class="w10">${department}</td>
					<td class="w10">${job}</td>
					<td class="w15">${email}</td>
					<td class="w15">${phone}</td>
					<td class="w20">${desc}</td>
					<td class="w12" userId="${id}">
						<a href="useradd.html?userId=${id}">编辑</a>
						<a href="javascript:;">删除</a>
						<a href="javascript:;">重置密码</a>
					</td>
				</tr>`;
			});
			$tbody.html(str);
			$checkList = $tbody.find('input[type="checkbox"]');
			$checkAll.prop('checked', false);
			return;
		}
		// 没有获取到数据（让TBODY清空：或者显示无数据的提示信息）
		$tbody.html('');
		$checkList = $tbody.find('input[type="checkbox"]');
		$checkAll.prop('checked', false);
	};

	// 筛选处理
	let handleFilter = function () {
		$selectBox.on('change', queryUserList);
		$searchInp.on('keydown', function (ev) {
			if (ev.keyCode === 13) {
				// ENTER
				queryUserList();
			}
		});
	};

	// 删除的操作处理
	let handleDelete = function () {
		// 单一删除
		$tbody.click(ev => {
			let target = ev.target,
				targetTagName = target.tagName,
				targetText = target.innerHTML,
				$target = $(target);

			// 删除的事件源
			if (targetTagName === 'A' && targetText === '删除') {
				let userId = $target.parent().attr('userId');
				alert(`您确定要删除编号为 ${userId} 的员工信息吗？`, {
					confirm: true,
					handled: async type => {
						// 点击的是确定
						if (type === 'CONFIRM') {
							let result = await axios.get('/user/delete', {
								params: {
									userId
								}
							});
							if (parseInt(result.code) === 0) {
								alert('删除成功!');
								$target.parent().parent().remove();
								return;
							}
							alert('删除失败，请稍后重试!');
						}
					}
				});
				return;
			}

			// 复选框的事件源
			if (targetTagName === 'INPUT') {
				let flag = true;
				_each($checkList, item => {
					if (!$(item).prop('checked')) {
						flag = false;
						return false; //=>结束循环
					}
				});
				$checkAll.prop('checked', flag);
			}
		});

		// 全选和全不选
		$checkAll.click(function () {
			$checkList.prop('checked', $checkAll.prop('checked'));
		});

		// 批量删除
		$deleteAll.click(function () {
			// 获取选中想的ID集合
			let arr = [];
			_each($checkList, item => {
				let $item = $(item);
				if ($item.prop('checked')) {
					arr.push($item.attr('userId'));
				}
			});
			if (arr.length === 0) {
				alert('请先选择要删除的项');
				return;
			}

			// 删除
			alert(`您确定要删除编号为 ${arr.join('、')} 的这些员工吗？`, {
				confirm: true,
				handled: type => {
					if (type === 'CONFIRM') {
						recursionDelete(arr);
					}
				}
			});
		});
	};

	let recursionDelete = async function (arr) {
		// 都删除了，重新从服务器获取员工信息
		if (arr.length === 0) {
			alert('批量删除成功!', {
				handled: _ => {
					queryUserList();
				}
			});
			return;
		}

		// 先取出第一项，就是我们要删除那一项
		let userId = arr[0];
		let result = await axios.get('/user/delete', {
			params: {
				userId
			}
		});
		if (parseInt(result.code) !== 0) {
			alert(`删除编号为 ${userId} 的员工失败，删除终止！`, {
				handled: _ => {
					queryUserList();
				}
			});
			return;
		}
		// 当前项删除成功，我们干掉第一项
		arr.shift();
		recursionDelete(arr);
	};

	return {
		init() {
			// 筛选部门
			queryDepartmentList();
			// 筛选用户
			queryUserList();
			handleFilter();
			// 删除
			handleDelete();
		}
	}
})();
userListModule.init();